package com.ht.pms.core.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.ht.pms.core.dao.SalaryBusinessDictDao;
import com.ht.pms.core.dto.dict.DictParamsDto;
import com.ht.pms.core.dto.dict.DictParamsReq;
import com.ht.pms.core.dto.tempalte.TemplateReq;
import com.ht.pms.core.handler.SalaryValidateDataHandler;
import com.ht.pms.core.service.SalaryBusinessDictService;

@Service
public class SalaryBusinessDictServiceImpl implements SalaryBusinessDictService {
    private static final Logger LOG = LoggerFactory.getLogger(SalaryBusinessDictServiceImpl.class);
    
    @Autowired
    private SalaryBusinessDictDao salaryBusinessDictDao;
    
    @Autowired
	private SalaryValidateDataHandler validateHandler;
    
    @Override
    public void addColumn() throws Exception {
        List<DictParamsDto> tableList = salaryBusinessDictDao.querySalaryBusinessDictTable();
        if (!CollectionUtils.isEmpty(tableList)) {
            for (DictParamsDto salaryBusinessDict : tableList) {
                String dictName = salaryBusinessDict.getDictName();
                String columnName = salaryBusinessDict.getDictColumn();
                String tableName = salaryBusinessDict.getDictTableName();
                String dictDataDes = salaryBusinessDict.getDictDataDesc();
                try{
                    salaryBusinessDictDao.addColumn(tableName,columnName,dictDataDes);
                }catch (Exception e){
                    LOG.error("=========>>>>>>>>>>>>>>>>出现错误{}",e);
                }
            }
        }
    }

	@Override
	public List<DictParamsDto> getDictParamList(TemplateReq req) {
		
		return salaryBusinessDictDao.selectDictParamList(req);
	}

	@Override
	public int getDictParamCount(TemplateReq req) {
		
		return salaryBusinessDictDao.selectDictParamCount(req);
	}
	
	@Transactional
	@Override
	public void saveDictParam(DictParamsReq req) {
		DictParamsDto dictParam = validateHandler.validateDictParam(req);
		if(StringUtils.isEmpty(req.getDictCode())) {
			// 查询C表最大的字段号
			DictParamsDto dict =  salaryBusinessDictDao.selectMaxDictParamByTableName(dictParam.getDictTableName());
			if( dict != null) {
				String[] dictCodes = dict.getDictCode().split("(?<=record*_)(?=\\d+)");
				String[] dictColums = dict.getDictColumn().split("(?<=column)(?=\\d+)");
				String dictCode =  dictCodes[0] + (Integer.valueOf(dictCodes[1]) + 1);
				String dictColum = dictColums[0] + (Integer.valueOf(dictColums[1]) + 1);
				dictParam.setDictCode(dictCode);
				dictParam.setDictColumn(dictColum);
				
				// 新增字段表字段
				salaryBusinessDictDao.insertDictParam(dictParam);
				
//				salaryBusinessDictDao.createField(dictParam);
			}
		}else {
			// 修改
			salaryBusinessDictDao.updateDictParam(dictParam);
		}
	}

	@Override
	public DictParamsDto getDictParam(String dictCode) {
		
		return salaryBusinessDictDao.selectDictParamByDictCode(dictCode);
	}
	
	public static void main(String[] args) {
		System.out.println(Arrays.toString("recordc_8".split("(?<=recordc_)(?=\\d+)")));
		String[] dictCodes = "column8".split("(?<=column)(?=\\d+)");
		System.out.println(dictCodes[0] + (Integer.valueOf(dictCodes[1]) + 1));
	}
}
